package com.report.repository;

import com.report.entity.Project;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface ProjectRepository extends JpaRepository<Project, String> {
    
    // 根据被审计单位信用代码查询
    List<Project> findByBsdwxydm(String bsdwxydm);
    
    // 根据上级单位信用代码查询子级企业
    List<Project> findBySjdwxydm(String sjdwxydm);
    
    // 根据集团公司信用代码查询所有关联企业
    List<Project> findByJtgsxydm(String jtgsxydm);

    List<Project> findByBsdw(String bsdw);

    // 递归查询所有子级企业
    @Query("SELECT p FROM Project p WHERE p.sjdwxydm = :xydm OR p.bsdwxydm IN " +
           "(SELECT p2.bsdwxydm FROM Project p2 WHERE p2.sjdwxydm = :xydm)")
    List<Project> findAllSubCompanies(String xydm);

    @Transactional
    void deleteBySjxmbh(String sjxmbh);

}
